package org.zyx.goods.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.zyx.goods.service.GoodsHtmlService;

import javax.annotation.Resource;

/**
 * @Author: SwordMaple
 * @Date: 2020/10/20  23:48
 * @Description: 监听商品服务发送的消息, 实现商品数据的同步
 * @Version: 1.0
 */
@Component
public class GoodsListener {

    @Resource
    private GoodsHtmlService goodsHtmlService;



    /**
     * 监听收到的消息(添加,更新商品操作),对数据(elasticsearch,静态页面)进行同步更新
     * @param spuId
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "YIYOU.ITEM.SAVE.QUEUE", durable = "true"),
            exchange = @Exchange(
                    value = "YIYOU.ITEM.EXCHANGE",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = {"item.insert","item.update"}))
    public void save(Long spuId) {
        if (spuId == null){
            return ;
        }
        //生成或覆盖静态页面
        goodsHtmlService.asyncExcute(spuId);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "YIYOU.ITEM.SAVE.QUEUE", durable = "true"),
            exchange = @Exchange(
                    value = "YIYOU.ITEM.EXCHANGE",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC),
            key = {"item.delete"}))
    public void delete(Long spuId) {
        if (spuId == null){
            return ;
        }
        //1.删除静态页面
        goodsHtmlService.deleteHtml(spuId);
        //2.

    }


}
